package org.smile.db.handler;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.smile.exception.SmileException;
import org.smile.io.IOUtils;

/**
 * 这个map 的值是不区分大小写的
 * 他的key只能是String类型的
 * @author strive
 *
 */
public class ResultSetMap extends LinkedHashMap<String,Object> {
	
		private static final DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
        private final Map<String, String> lowerCaseMap = new HashMap<String, String>();

        /**
         * Required for serialization support.
         *
         * @see java.io.Serializable
         */
        private static final long serialVersionUID = -2848100435296897392L;

        /** {@inheritDoc} */
        @Override
        public boolean containsKey(Object key) {
            Object realKey = lowerCaseMap.get(key.toString().toLowerCase());
            return super.containsKey(realKey);
        }

        /** {@inheritDoc} */
        @Override
        public Object get(Object key) {
            Object realKey = lowerCaseMap.get(key.toString().toLowerCase());
            return super.get(realKey);
        }
        /** {@inheritDoc} */
        @Override
        public Object put(String key, Object value){
            Object oldKey = lowerCaseMap.put(key.toLowerCase(), key);
            Object oldValue = super.remove(oldKey);
            super.put(key, value);
            return oldValue;
        }
        /**
         * byte数组形式存入
         * @param key
         * @param value 可以是输入流、字符串
         * @throws SmileException 
         * @throws IOException
         */
        public void putBytes(String key,Object value) throws SmileException{
        	if(value instanceof byte[]){
        		this.put(key, value);
        	}else if(value instanceof String){
        		this.put(key, ((String)value).getBytes());
        	}else{
        		throw new SmileException("未预知的转换对象："+value.getClass().getName()+" to byte[]");
        	}
        }
        /**
         * 把一个流以byte数据形式存入数据库
         */
        public void putBytes(String key,InputStream is) throws IOException{
        	this.put(key,IOUtils.stream2byte(is));
        }
        /** {@inheritDoc} */
        @Override
        public void putAll(Map<? extends String, ?> m) {
            for (Map.Entry<? extends String, ?> entry : m.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                this.put(key, value);
            }
        }
        /**
         * 以btye[] 返回
         * @param key
         * @return
         * @throws SQLException
         */
        public byte[] getBytes(String key) throws SQLException{
        	Object value=get(key);
        	if(value==null){
        		return null;
        	}else if(value instanceof String){
        		return  ((String)value).getBytes();
        	}else if( value instanceof Blob){
        		Blob blob =(Blob)value;
        		return blob.getBytes(1, (int)blob.length());
        	}else if(value instanceof Clob){
        		Clob clob=(Clob)value;
        		int len=(int)clob.length();
        		String str=clob.getSubString(1, len);
        		return str.getBytes();
        	}else if(value instanceof byte[]){
        		return (byte[])value;
        	}else if(value instanceof Byte[]){
        		Byte[] bt=(Byte[])value;
        		byte[] b=new byte[bt.length];
        		for(int i=0;i<b.length;i++){
        			b[i]=bt[i].byteValue();
        		}
        		return b;
        	}else{
        		throw new SQLException("不支持的属性");
        	}
        }
        /**
         * 以String方式返回
         * @param key
         * @return
         * @throws SQLException
         */
        public String getString(String key) throws SQLException{
        	Object value=get(key);
        	if(value==null){
        		return null;
        	}else if(value instanceof String){
        		return  (String)value;
        	}else if(value instanceof Clob){
        		Clob clob=(Clob)value;
        		int len=(int)clob.length();
        		String str=clob.getSubString(1, len);
        		return str;
        	}else if(value instanceof Blob){
        		Blob blob =(Blob)value;
        		return new String(blob.getBytes(1, (int)blob.length()));
        	}else if(value instanceof byte[]){
        		return new String((byte[])value);
        	}else if(value instanceof Byte[]){
        		Byte[] bt=(Byte[])value;
        		byte[] b=new byte[bt.length];
        		for(int i=0;i<b.length;i++){
        			b[i]=bt[i].byteValue();
        		}
        		return new String(b);
        	}else if(value instanceof Date){
        		return df.format((Date)value);
        	}else{
        		return new String(value+"");
        	}
        }
        
        /** {@inheritDoc} */
        @Override
        public Object remove(Object key) {
            Object realKey = lowerCaseMap.remove(key.toString().toLowerCase());
            return super.remove(realKey);
        }
	
}
